﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HooYoo.Web.Models;
using NHibernate.Criterion;
using System.ComponentModel;
using HooYoo.Web.Helpers;
namespace HooYoo.Web.Evada.Controllers {
    [HandleError]
    public class CasesController : ControllerBase {
        public ActionResult Index(string keyword, [DefaultValue(1)]int lableId, [DefaultValue(1)]int pageIndex, string view) {

            var list = Case.FindAll(

                    ).ToList();


            var model = new Cases.IndexModel();
            model.PagedList = model.PagedFindAll(pageIndex, new[] { Order.Desc("CreateDT") },
                   Expression.And(
                      Expression.Eq("Lable", new Lable() { LableId = lableId })
                      ,
                      !string.IsNullOrEmpty(keyword) ? Expression.Like("Title", "%" + keyword + "%") : Expression.Sql("1=1")
                      )
                      );
            model.Lable = Lable.Find(lableId);
            return View(view, model);
        }

        public ActionResult Index([DefaultValue(1)]int pageIndex) {
            return Index(null, 1, pageIndex, null);
        }

        public ActionResult Index(int lableId, [DefaultValue(1)]int pageIndex) {
            return Index(null, lableId, pageIndex, null);
        }

        [HttpPost]
        public ActionResult Search(string keyword) {
            var id = Case.FindFirst(Expression.Eq("Lable", new Lable() { LableId = 4 }));
            return Details(id.CaseId, "product", keyword);
        }

        public ActionResult Index() {
            return Index(null, 1, 1, null);
        }


        public ActionResult Network() {
            return Index(8, 1);
        }
        public ActionResult Networks([DefaultValue(11)]int id) {
            return Index(id, 1);
        }
        public ActionResult Products([DefaultValue(4)]int id) {
            return Details(id);
        }

        public ActionResult Solutions([DefaultValue(21)]int id) {
            return Details(id);
        }
        public ActionResult Alliagces([DefaultValue(18)]int id) {
            return Details(id, "Details");
        }

        public ActionResult Services([DefaultValue(19)]int id) {
            return Details(id);
        }

        public ActionResult Abouts([DefaultValue(7)]int id) {
            return Details(id);
        }

        public ActionResult Product() {
            var id = Case.FindFirst(Expression.Eq("Lable", new Lable() { LableId = 4 }));
            return Details(id.CaseId);
        }

        //public ActionResult Solution(int id) {
        //    return Details(id);
        //}

        //public ActionResult Alliagce(int id) {
        //    return Details(id);
        //}

        //public ActionResult Service(int id) {
        //    return Details(id, null);
        //}

        public ActionResult Details(int id, string viewName, string keyword) {
            var model = new Cases.CaseModel() {
                Model = Case.FindOne(Expression.IdEq(id))
            };
            if (model.Model == null) return RedirectToAction("Index");
            model.Lable = model.Model.Lable;
            model.Cases = Case.FindAll(
            Expression.Eq("Lable", model.Lable),
            !string.IsNullOrEmpty(keyword) ? Expression.Like("Title", "%" + keyword + "%") : Expression.Sql("1=1")
            ).ToList();
            return View(viewName, model);
        }
        public ActionResult Details(int id, string viewName) {
            return Details(id, viewName, null);
        }
        public ActionResult Details(int id) {
            return Details(id, null, null);
        }

    }
}
